package cn.dengta.webapp.im.dto;

import java.util.HashMap;
import java.util.Map;

import cn.dengta.context.model.WesKit;
import cn.dengta.context.rpc.CachedConfig;
import cn.dengta.context.rpc.CachedConfigManager;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.Assert;
import me.codeplayer.util.X;
import org.springframework.beans.factory.InitializingBean;

@Getter
@Setter
public class ImPriceConfig implements InitializingBean {

	private Map<WesKit, ImPrice> imPrice;

	public static CachedConfig<ImPriceConfig> cache() {
		return InstanceHolder.instance;
	}

	public static ImPrice buildImPrice(WesKit wesKit) {
		return cache().get().getImPrice().get(wesKit);
	}

	public static ImPriceConfig getDefault() {
		final ImPriceConfig cfg = new ImPriceConfig();
		cfg.imPrice = new HashMap<>();
		for (WesKit wesKit : WesKit.values()) {
			cfg.imPrice.put(wesKit, ImPrice.getDefault());
		}
		return cfg;
	}

	@Override
	public void afterPropertiesSet() {
		Assert.isTrue(X.isValid(imPrice), "请设置数据");
		for (Map.Entry<WesKit, ImPrice> entry : this.imPrice.entrySet()) {
			entry.getValue().check();
		}
	}

	private static final class InstanceHolder {

		static final CachedConfig<ImPriceConfig> instance = CachedConfigManager.create(ImPriceConfig.class, ImPriceConfig::getDefault);

	}

	@Getter
	@Setter
	public static class ImPrice {

		/** 搭讪消息单价：钻石/次 */
		Integer greetPrice;
		/** 普通（图文）消息单价：钻石/条 */
		Integer msgPrice;
		/** 语音聊天单价：钻石/分钟 */
		Integer audioPrice;
		/** 视频聊天单价：钻石/分钟 */
		Integer videoPrice;

		/** 普通（图文）【群聊】消息单价：钻石/条 */
		Integer groupMsgPrice;

		/** 每天可免费搭讪次数 */
		int freeTimesOfGreetEveryday;
		/** 每天可免费信息聊天次数 */
		int freeTimesOfMsgEveryday;
		/** VIP每天可免费信息聊天次数 */
		int freeTimesOfMsgEverydayVIP = 20;
		/** 牵线价格 */
		Integer pullWirePrice = 2;

		public static ImPrice getDefault() {
			final ImPrice cfg = new ImPrice();
			cfg.greetPrice = 0;
			cfg.msgPrice = 6;
			cfg.audioPrice = 42;
			cfg.videoPrice = 63;
			cfg.groupMsgPrice = 0;

			cfg.freeTimesOfGreetEveryday = 6;
			cfg.freeTimesOfMsgEveryday = 8;
			cfg.freeTimesOfMsgEverydayVIP = 20;
			return cfg;
		}

		public void check() {
			Assert.isTrue(greetPrice >= 0 && msgPrice >= 0 && audioPrice >= 0 && videoPrice >= 0 && groupMsgPrice >= 0 && pullWirePrice >= 0, "聊天价格配置有误！");
		}

	}

}
